<?php

namespace App\Http\Controllers\Auth;

use App\Models\Admin;
use App\Models\AdminRole;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    /** 管理员 */
    public function index(Request $request)
    {
        if ($request->isMethod('post')) {
            $model = Admin::paginate(config('base.limit'));

            return $this->data($model->items(), $model->total());
        }
        return view('auth.admin.index');
    }

    /** 添加管理员 */
    public function edit(Request $request)
    {
        $id = $request->get('id');

        if (!empty($id)) {
            $model = Admin::find($id);

        } else {
            $model = new Admin();
        }

        if ($request->isMethod('post')) {
            $data = array_filter($request->all());

            /** 基础验证 */
            $validator = $this->validateLogin($data);
            if ($validator->fails()) {
                return $this->fail($validator->errors()->first());
            }

            $model->username = $data['username'];
            $model->nickname = $data['nickname'];
            $model->email = $data['email'] ?? null;

            /** 密码验证 */
            if (empty($id)) {
                if (empty($data['password'])) return $this->fail('密码不得为空');

                if ($data['password'] != $data['password_confirmation']) return $this->fail('两次密码不一致');

                if (strlen($data['password']) > 16 || strlen($data['password']) < 6) return $this->fail('密码必须为6~16位');

                $model->password = Hash::make($data['password']);
            }
            $model->save();

            /** 设置权限 */
            AdminRole::updateOrCreate(['admin_id' => $model->id], [
                'admin_id' => $model->id,
                'role_id' => $data['role_id']
            ]);


            return $this->success('操作成功!');

        }

        $model->role_id = AdminRole::where('admin_id', $id)->value('role_id');
        $role = Role::pluck('name', 'id')->toArray();
        return view('auth.admin.edit', ['model' => $model, 'role' => $role, 'id' => $id]);
    }

    /** 重置密码 */
    public function reset(Request $request)
    {
        $id = $request->get('id');

        if ($request->isMethod('post')) {
            $data = $request->all();
            if (empty($data['password'])) return $this->fail('密码不得为空');

            if ($data['password'] != $data['password_confirmation']) return $this->fail('两次密码不一致');

            if (strlen($data['password']) > 16 || strlen($data['password']) < 6) return $this->fail('密码必须为6~16位');

            $model = Admin::find($id);
            $model->password = Hash::make($data['password']);

            if ($model->save()) return $this->success('修改成功!');

        }

        return view('auth.admin.reset', ['id' => $id]);
    }

    /** 删除管理员 */
    public function del(Request $request)
    {

    }

    /** 添加账号验证 */
    protected function validateLogin($data)
    {
        $validator = Validator::make($data, [
            'username' => 'required',
            'nickname' => 'required',
            'role_id' => 'required|exists:roles,id',
            'email' => 'sometimes|email',
        ], [
            'required' => ':attribute不得为空',
            'exists' => ':attribute不存在',
            'email' => ':attribute格式不正确'
        ], [
            'username' => '登录账号',
            'nickname' => '昵称',
            'role_id' => '角色',
            'email' => '电子邮箱'
        ]);
        return $validator;
    }
}
